/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2022 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "fatfs.h"
#include "i2c.h"
#include "sdio.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "fsmc.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "ILI93xx.h"
#include "lvgl.h"
#include "lv_port_disp.h"
#include "lv_port_indev.h"
#include "lv_port_fs.h"
#include "mainpage.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
unsigned int count = 0;
BYTE map[5] = {0};
void TestSD()
{
    FRESULT f_res; //专门用于装返回�??
    FIL file;      //文件指针
    FATFS SD;      //设备
    int len1, len2;

    //挂载储存�????????
    f_res = f_mount(&SD, "0:", 1);
    if (f_res == FR_OK)
        UART_printf(&huart1, "SD mount OK!\r\n");
    else
        UART_printf(&huart1, "SD mount error = %d\r\n", f_res);

    // FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_CREATE_NEW | FA_OPEN_ALWAYS | FA_OPEN_APPEND | FA_SEEKEND
    // f_res = f_open(&file,"0:test1.txt", FA_WRITE | FA_CREATE_ALWAYS);

    //以写入方式打�????????test1.txt文件，如果不存在则创建该文件
    f_res = f_open(&file, "0:test1.txt", FA_CREATE_ALWAYS | FA_WRITE | FA_READ);
    if (f_res == FR_OK)
        UART_printf(&huart1, "'test1.txt' Open OK!\r\n");
    else
        UART_printf(&huart1, "'test1.txt' Open error = %d\r\n", f_res);

    //移动到文件末�????????
    f_res = f_lseek(&file, (&file)->fptr + 10);

    //数据写入文件
    count++;
    len1 = f_printf(&file, "%s", "0123456789\t"); //?????
    len2 = f_printf(&file, "%d", count);          //?????

    if (len1 >= 0)
        UART_printf(&huart1, "Write OK!write size = %d\t%d\r\n", (len1 + len2), count);
    else
        UART_printf(&huart1, "Write error!\r\n");

    //写完文件后，关闭文件
    f_res = f_close(&file); //?????,??????,??????
    if (f_res == FR_OK)
        UART_printf(&huart1, "'test1.txt' Close OK!\r\n");
    else
        UART_printf(&huart1, "'test1.txt' Close error = %d\r\n", f_res);

    //取消挂载存储�????????
    f_res = f_mount(NULL, "0:", 1);
    if (f_res == FR_OK)
        UART_printf(&huart1, "SD unmount OK!\r\n");
    else
        UART_printf(&huart1, "SD unmount error = %d\r\n", f_res);

    UART_printf(&huart1, "\r\n");
}
#ifdef Load_MAP
void load()
{
    FATFS SD_0;
    FRESULT frl;
    FIL mapData;

    f_mount(&SD_0, "0:", 1);
    if (frl == FR_OK)
        UART_printf(&huart1, "SD mount OK!\r\n");
    else
        UART_printf(&huart1, "SD mount error = %d\r\n", frl);

    frl = f_open(&mapData, "0:map.txt", FA_CREATE_ALWAYS | FA_WRITE | FA_READ);

    frl = f_write(&mapData, map, sizeof(map), NULL);

    f_close(&mapData);
    if (frl == FR_OK) {
        UART_printf(&huart1, "'map.txt' Close OK!\r\n");
        UART_printf(&huart1, "%d,%d\n", map[0], map[4]);
    } else
        UART_printf(&huart1, "'map.txt' Close error = %d\r\n", frl);

    f_mount(NULL, "0:", 1);
}
void read()
{
    FATFS SD_0;
    FRESULT frr;
    FIL mapData;

    f_mount(&SD_0, "0:", 1);
    if (frr == FR_OK)
        UART_printf(&huart1, "SD mount OK!\r\n");
    else
        UART_printf(&huart1, "SD mount error = %d\r\n", frr);
    UART_printf(&huart1, "%d,%d\n", map[0], map[4]);

    frr = f_open(&mapData, "0:map.txt", FA_READ);

    frr = f_read(&mapData, map, sizeof(map), NULL);

    f_close(&mapData);
    if (frr == FR_OK) {
        UART_printf(&huart1, "'map.txt' Close OK!\r\n");
        UART_printf(&huart1, "%d,%d\n", map[0], map[4]);
    } else {
        UART_printf(&huart1, "'map.txt' Close error = %d\r\n", frr);
        // UART_printf(&huart1, "%d,%d\n", map[0], map[4]);
    }

    f_mount(NULL, "0:", 1);
}
#endif
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_FSMC_Init();
  MX_TIM3_Init();
  MX_USART1_UART_Init();
  MX_SDIO_SD_Init();
  MX_FATFS_Init();
  MX_I2C1_Init();
  MX_I2C2_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
    HAL_TIM_Base_Start_IT(&htim3);

    HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, 1);

    // HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,1);
    if(infoBox.enableBGM)
        FirstSong();
    HAL_Delay(300);
    
    lv_init();            // LVGL初始化
    lv_port_disp_init();  //显示器初始化
    lv_port_indev_init(); //显示器初始化
    lv_port_fs_init();    //文件系统初始化?

    settingInfoInit();//设置配置文件读取
    mygame_init();//游戏文件读取

    starting_up();//加载界面入口

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
    while (1) {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
        lv_task_handler(); // LVGL任务处理
    }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{

    if (htim->Instance == htim3.Instance) {
        // HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_9);
        lv_tick_inc(1);
    }
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    __disable_irq();
    while (1) {
    }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
